package com.useeinfo.oa.modules.task.entity;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.entity.SuperClassEntity;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.entity.DictInfo;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.security.entity.SystemUser;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import java.util.Optional;

/**
 * @auther: bin.liu
 * @Date: 2019/7/1 10:18
 * @Description: 任务工单参与人
 */
@Entity
@Table(name = "task_order_partner")
public class TaskOrderPartner extends SuperClassEntity<TaskOrderPartner> {

    /**
     * 申请原因
     */
    private String reason;

    /**
     * 协作类型
     */
    @ManyToOne
    @JoinColumn(name = "teamwork_cat")
    private DictInfo teamworkCat;

    @ManyToOne
    @JoinColumn(name = "task_order_id")
    private  TaskOrder taskOrder;

    @ManyToOne
    @JoinColumn(name = "enterprise_id")
    private Enterprise enterprise;

    @ManyToOne
    @JoinColumn(name = "partner_id")
    private SystemUser partner;

    /**
     * 我的工时
     */
    private long workingHours;

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public DictInfo getTeamworkCat() {
        return teamworkCat;
    }

    public void setTeamworkCat(DictInfo teamworkCat) {
        this.teamworkCat = teamworkCat;
    }

    public TaskOrder getTaskOrder() {
        return taskOrder;
    }

    public void setTaskOrder(TaskOrder taskOrder) {
        this.taskOrder = taskOrder;
    }

    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

    public SystemUser getPartner() {
        return partner;
    }

    public void setPartner(SystemUser partner) {
        this.partner = partner;
    }

    public long getWorkingHours() {
        return workingHours;
    }

    public void setWorkingHours(long workingHours) {
        this.workingHours = workingHours;
    }

    @Override
    public JSONObject toJSONObject() {
        JSONObject jsonObject =  super.toJSONObject();
        jsonObject.put("partnerName", Optional.ofNullable(getPartner()).map(SystemUser::getUserName).orElse(""));
        jsonObject.put("mobile", Optional.ofNullable(getPartner()).map(SystemUser::getMobile).orElse(""));
        jsonObject.put("reason", StringConverters.null2String(getReason()));
        jsonObject.put("teamworkCatName", Optional.ofNullable(getTeamworkCat()).map(DictInfo::getPropertyName).orElse(""));
        jsonObject.put("workingHours", StringConverters.null2String(getWorkingHours()));
        return jsonObject;
    }
}
